
package org.dyndns.cooland.downloader;

import java.awt.AWTException;
import java.awt.BorderLayout;
import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.SystemTray;
import java.awt.TrayIcon;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowStateListener;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.JToolBar;
import javax.swing.ListSelectionModel;
import javax.swing.SwingConstants;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.table.DefaultTableModel;

public class Downloader extends JFrame {
	
	private static final long	serialVersionUID	= -2683851726995285792L;
	private SystemTray			systemTray			= SystemTray
															.getSystemTray();
	private TrayIcon			trayIcon;
	private JTable				tpConnections;
	
	private void removeTrayIcon() {

		systemTray.remove(trayIcon);
	}
	
	private void addTrayIcon() {

		try{
			systemTray.add(trayIcon);
			trayIcon.displayMessage("Orbiter Updater",
					"Downloader minimized to tray, but still active",
					TrayIcon.MessageType.INFO);
		}
		catch (AWTException e){
			e.printStackTrace();
		}
	}
	
	public Downloader () throws IOException, AWTException {

		super("Orbiter Updater");
		setDefaultCloseOperation(JDialog.EXIT_ON_CLOSE);
		{
			JMenuBar menuBar = new JMenuBar();
			setJMenuBar(menuBar);
			{
				JMenu mnFile = new JMenu("File");
				menuBar.add(mnFile);
				{
					JMenuItem mntmExit = new JMenuItem("Exit");
					mntmExit.addActionListener(new ActionListener() {
						
						public void actionPerformed(ActionEvent e) {

							System.exit(0);
						}
					});
					mnFile.add(mntmExit);
				}
			}
			{
				JMenu mnPreferences = new JMenu("Preferences");
				menuBar.add(mnPreferences);
				{
					JMenuItem mntmPreferences = new JMenuItem("Preferences");
					mntmPreferences.addActionListener(new ActionListener() {
						
						public void actionPerformed(ActionEvent e) {

							PreferencesDlg dlg = new PreferencesDlg();
							dlg.setVisible(true);
						}
					});
					mnPreferences.add(mntmPreferences);
				}
			}
			{
				JMenu mnHelp = new JMenu("Help");
				menuBar.add(mnHelp);
				{
					JMenuItem mntmAbout = new JMenuItem("About");
					mnHelp.add(mntmAbout);
				}
			}
		}
		{
			JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.BOTTOM);
			getContentPane().add(tabbedPane, BorderLayout.CENTER);
			{
				tpConnections = new JTable();
				tpConnections.setAutoscrolls(false);
				tpConnections.setShowGrid(false);
				tpConnections
						.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
				tpConnections.setCellSelectionEnabled(true);
				tpConnections.setModel(new DefaultTableModel(new Object[][] { {
						"orbiter.com/catalog.zip", "10:05", "", "10 kB" } },
						new String[] { "Target", "Time", "Status",
								"Bytes Recieved" }) {
					
					Class[]	columnTypes	= new Class[] { String.class,
												String.class, String.class,
												String.class };
					
					public Class getColumnClass(int columnIndex) {

						return columnTypes[columnIndex];
					}
				});
				tpConnections.getColumnModel().getColumn(0).setPreferredWidth(
						153);
				tpConnections.getColumnModel().getColumn(2).setPreferredWidth(
						147);
				tpConnections.getColumnModel().getColumn(3).setPreferredWidth(
						126);
				tabbedPane.addTab("Connections", null, tpConnections, null);
				tabbedPane.setEnabledAt(0, true);
			}
			{
				JTextArea tpStatistics = new JTextArea();
				tabbedPane.addTab("Statistics", null, tpStatistics, null);
				tabbedPane.setEnabledAt(1, true);
			}
			{
				JPanel tpTraffic = new JPanel();
				tabbedPane.addTab("Traffic", null, tpTraffic, null);
				tabbedPane.setEnabledAt(2, true);
			}
			{
				JTextArea tpLog = new JTextArea();
				tabbedPane.addTab("Log", null, tpLog, null);
				tabbedPane.setEnabledAt(3, true);
			}
		}
		{
			JToolBar toolBar = new JToolBar();
			toolBar.setFloatable(false);
			toolBar.setOrientation(SwingConstants.VERTICAL);
			getContentPane().add(toolBar, BorderLayout.WEST);
			{
				JButton btnToolBar1 = new JButton("");
				btnToolBar1
						.setIcon(new ImageIcon(
								Downloader.class
										.getResource("/javax/swing/plaf/basic/icons/JavaCup16.png")));
				toolBar.add(btnToolBar1);
			}
			{
				JButton btnToolBar2 = new JButton("");
				btnToolBar2
						.setIcon(new ImageIcon(
								Downloader.class
										.getResource("/javax/swing/plaf/basic/icons/JavaCup16.png")));
				toolBar.add(btnToolBar2);
			}
			{
				JButton btnToolBar3 = new JButton("");
				btnToolBar3
						.setIcon(new ImageIcon(
								Downloader.class
										.getResource("/javax/swing/plaf/basic/icons/JavaCup16.png")));
				toolBar.add(btnToolBar3);
			}
		}
		trayIcon = new TrayIcon(ImageIO.read(this.getClass()
				.getResourceAsStream("/zip.png")), "Orbiter Updater");
		trayIcon.addActionListener(new ActionListener() {
			
			public void actionPerformed(ActionEvent e) {

				if (!isVisible()) setVisible(true);
				setState(JFrame.NORMAL);
				removeTrayIcon();
			}
		});
		
		addWindowStateListener(new WindowStateListener() {
			
			public void windowStateChanged(WindowEvent e) {

				if (e.getNewState() == JFrame.ICONIFIED){
					setVisible(false);
					addTrayIcon();
				}
			}
		});
		
		PopupMenu popupMenu = new PopupMenu();
		MenuItem item = new MenuItem("Exit");
		item.addActionListener(new ActionListener() {
			
			public void actionPerformed(ActionEvent e) {

				dispose();
				System.exit(0);
			}
		});
		popupMenu.add(item);
		trayIcon.setPopupMenu(popupMenu);
		// systemTray.add(trayIcon);
	}
	
	/**
	 * @param args
	 * @throws UnsupportedLookAndFeelException
	 * @throws IllegalAccessException
	 * @throws InstantiationException
	 * @throws ClassNotFoundException
	 */
	public static void main(String[] args) throws IOException, AWTException,
			ClassNotFoundException, InstantiationException,
			IllegalAccessException, UnsupportedLookAndFeelException {

		UIManager
				.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
		Downloader app = new Downloader();
		app.pack();
		app.setLocationRelativeTo(null);
		app.setVisible(true);
	}
	
}
